import { _decorator, Component, Node, Prefab, instantiate } from 'cc';
import { AudioManager } from '../../framework/manager/AudioManager';
import { Balloon } from './ctrl/Balloon';
const { ccclass, property } = _decorator;

@ccclass('BoomGame')
export class BoomGame extends Component {

    @property(Prefab)
    public balloonPrefab: Prefab

    onLoad(): void {
        // 加上背景音乐
        AudioManager.Instance.stop()
        AudioManager.Instance.playMusic("boom/sounds/bg")
    }

    start(): void {
        this.scheduleOnce(this.genBalloon.bind(this), 0.5)
    }

    genBalloon() {
        var type = 1 + Math.random() * 7;
        type = Math.floor(type);
        if (type > 7) {
            type = 7;
        }

        var speed = 200 + Math.random() * 300; // 气球移动的速度
        var balloon = instantiate(this.balloonPrefab);
        balloon.active = true;
        this.node.addChild(balloon);

        // [ -270, 270]
        var xpos = (Math.random() - 0.5) * 540;
        // [0, -400]
        var ypos = Math.random() * -400;
        // 随机位置
        balloon.setPosition(xpos, ypos, 0)

        var balloonCtrl = balloon.getComponent(Balloon);
        balloonCtrl.genBalloon(type, speed);

        var time = Math.random() * 0.5 + 1;
        this.scheduleOnce(this.genBalloon.bind(this), time);
    }
}


